﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace WeSpot.Models
{
    public static class ElggUtils
    {
        /// <summary>
        /// Reponse with array
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static elgg RestRequest(string uri)
        {
            WeSpot.Models.elgg elggList;
            try
            {
                WebRequest request = WebRequest.Create(uri);

                WebResponse response = request.GetResponse();

                

                using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                {

                    XmlSerializer serializer = new XmlSerializer(typeof(WeSpot.Models.elgg));

                    elggList = (WeSpot.Models.elgg)serializer.Deserialize(sr);
                    sr.Close();
                }
            }
            catch (Exception ex)
            {
                elggList = new elgg();
            }
            return elggList;
        }

        public static ElggString.elgg RestRequestString(string uri)
        {
            WeSpot.Models.ElggString.elgg elggList;
            try
            {
                WebRequest request = WebRequest.Create(uri);

                WebResponse response = request.GetResponse();



                using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                {

                    XmlSerializer serializer = new XmlSerializer(typeof(WeSpot.Models.ElggString.elgg));

                    elggList = (WeSpot.Models.ElggString.elgg)serializer.Deserialize(sr);
                    sr.Close();
                }
            }
            catch (Exception ex)
            {
                elggList = new ElggString.elgg();
            }
            return elggList;
        }

        /// <summary>
        /// Result single
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static ElggInquiryArlearnRunModels.elgg RestRequestSingle(string uri)
        {
            WebRequest request = WebRequest.Create(uri);

            WebResponse response = request.GetResponse();

            WeSpot.Models.ElggInquiryArlearnRunModels.elgg elggList;

            using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
            {

                XmlSerializer serializer = new XmlSerializer(typeof(WeSpot.Models.ElggInquiryArlearnRunModels.elgg));

                elggList = (WeSpot.Models.ElggInquiryArlearnRunModels.elgg)serializer.Deserialize(sr);
                sr.Close();
            }

            return elggList;
        }


        /// <summary>
        /// Returns the user activity that has taken place in the past x minutes.
        /// </summary>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public static elgg GetUserActivity(int minutes)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=user.activity&api_key=" + Properties.Settings.Default.ElggApiKey + "&minutes=" + minutes + "";

            return RestRequest(uri);
        }

        /// <summary>
        /// Returns all the inquiries of the site.
        /// </summary>
        /// <returns></returns>
        public static elgg GetSiteInquiries()
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=site.inquiries&api_key=" + Properties.Settings.Default.ElggApiKey + "";

            return RestRequest(uri);
        }
        
        /// <summary>
        /// Returns all the users of the site.
        /// </summary>
        /// <returns></returns>
        public static elgg GetSiteUsers()
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=site.users&api_key=" + Properties.Settings.Default.ElggApiKey + "";

            return RestRequest(uri);
        }
        
        /// <summary>
        /// Returns the users associated to an inquiry.
        /// </summary>
        /// <param name="inquiryId"></param>
        /// <returns></returns>
        public static elgg GetInquiryUsers(string inquiryId)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=inquiry.users&api_key=" + Properties.Settings.Default.ElggApiKey + "&inquiryId=" + inquiryId + ""; ;

            return RestRequest(uri);
        }

        /// <summary>
        /// Returns the inquiries associated to a user.
        /// </summary>
        /// <param name="oauthId"></param>
        /// <param name="oauthProvider"></param>
        /// <returns></returns>
        public static elgg GetUserInquiries(string oauthId, string oauthProvider)
        {
            //string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=user.inquiries&api_key=" + Properties.Settings.Default.ElggApiKey + "&oauthId=" + oauthId + "&oauthProvider=" + oauthProvider + "";
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=user.inquiriesAdmin&api_key=" + Properties.Settings.Default.ElggApiKey + "&oauthId=" + oauthId + "&oauthProvider=" + oauthProvider + "";
            return RestRequest(uri);
        }

        /// <summary>
        /// Returns the friends of a user.
        /// </summary>
        /// <param name="oauthId"></param>
        /// <param name="oauthProvider"></param>
        /// <returns></returns>
        public static elgg GetUserFriends(string oauthId, string oauthProvider)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=user.friends&api_key=" + Properties.Settings.Default.ElggApiKey + "&oauthId=" + oauthId + "&oauthProvider=" + oauthProvider + "";

            return RestRequest(uri);
        }

        /// <summary>
        /// Returns the ARLearn run associated to an inquiry.
        /// </summary>
        /// <param name="inquiryId"></param>
        /// <returns></returns>
        public static ElggInquiryArlearnRunModels.elgg GetInquiryArlearnRun(string inquiryId)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=inquiry.arlearnrun&api_key=" + Properties.Settings.Default.ElggApiKey + "&inquiryId=" + inquiryId + ""; ;

            return RestRequestSingle(uri);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inquiryId"></param>
        /// <returns></returns>

        public static ElggInquiryArlearnRunModels.elgg GetInquiryArlearnGame(string inquiryId)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=inquiry.arlearngame&api_key=" + Properties.Settings.Default.ElggApiKey + "&inquiryId=" + inquiryId + ""; ;

            return RestRequestSingle(uri);
        }

        /// <summary>
        /// Returns the email of a user.
        /// </summary>
        /// <param name="oauthId"></param>
        /// <param name="oauthProvider"></param>
        /// <returns></returns>
        public static ElggString.elgg GetUserMail(string oauthId, string oauthProvider)
        {
            string uri = @"" + Properties.Settings.Default.ElggUrl + "?method=user.email&api_key=" + Properties.Settings.Default.ElggApiKey + "&oauthId=" + oauthId + "&oauthProvider=" + oauthProvider + "";

            return RestRequestString(uri);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Serialize<T>(T value)
        {

            if (value == null)
            {
                return null;
            }

            XmlSerializer serializer = new XmlSerializer(typeof(T));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
            settings.Indent = false;
            settings.OmitXmlDeclaration = false;

            using (StringWriter textWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    serializer.Serialize(xmlWriter, value);
                }
                return textWriter.ToString();
            }
        }
    }
}